﻿// Allows to set a class when the view is highlighted as well as handles events on highlight and unhighlight
define([
    'Wjs/Wjs'
],
function () {
    'use strict';

    return Wjs.defineOnce({

        type: 'Highlightable',

        internal: {

            initialize: function () {

                this.setHighlightableClass('wjs-highlightable');

                this.addEvents({

                    mouseenter: function (evt) {

                        this.highlighted = true;

                        if (this.highlightHandler) {

                            this.highlightHandler(evt);
                        }
                    },

                    mouseleave: function (evt) {

                        this.highlighted = false;

                        if (this.unhighlightHandler) {

                            this.unhighlightHandler(evt);
                        }
                    }

                });
            },

            listeners: {

                onCloned: function (cloned) {

                    this.copyHighlightableTo(cloned);
                }
            }
        },

        setHighlightableClass: function (cls) {

            if (this.highlightableClass) {

                this.replaceClass(this.highlightableClass, cls);
            }
            else {

                this.addClass(cls);
            }

            this.highlightableClass = cls;

            return this;
        },

        onHighlight: function (fcn) {

            this.highlightHandler = Wjs.chain(fcn, this.highlightHandler);

            return this;
        },

        onUnhighlight: function (fcn) {

            this.unhighlightHandler = Wjs.chain(fcn, this.unhighlightHandler);

            return this;
        },

        copyHighlightableTo: function (view) {
           
            view.setHighlightableClass(this.highlightableClass);

            view.highlightHandler = this.highlightHandler;

            view.unhighlightHandler = this.unhighlightHandler;
        }

    });

});